home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 114_01.zip / ED2.BDS < prev    next >
Text File  |  1993-06-01  |  9KB  |  509 lines

  1. /* Screen editor:  main program -- BDS C version
  2.  *
  3.  * Source:  ed2.bds
  4.  * Version: December 20, 1981.
  5.  * Transliteration of small-C version of September 5, 1981
  6.  */
  7.  
  8. /* define globals */
  9.  
  10. #include ed.h
  11. #include bdscio.h
  12. #include ed1.ccc
  13. #include edext.cc
  14.  
  15. /* define signon message */
  16.  
  17. #define SIGNON "BDS C editor, version 2:  December 20, 1981."
  18.  
  19. /* the main program dispatches the routines that
  20.  * handle the various modes.
  21.  */
  22.  
  23. #define CMNDMODE 1    /* enter command mode flag */
  24. #define INSMODE  2    /* enter insert modes flag */
  25. #define EDITMODE 3    /* enter edit mode flag */
  26. #define EXITMODE 4    /* exit editor flag */
  27.  
  28. main()
  29. {
  30. int mode;
  31.     /* fmt output by default goes to screen */
  32.     fmtassn(NO);
  33.     /* set tabs, clear the screen and sign on */
  34.     fmtset(8);
  35.     outclr();
  36.     outxy(0,SCRNL1);
  37.     message(SIGNON);
  38.     outxy(0,1);
  39.     /* clear filename [] for save(), resave() */
  40.     name("");
  41.     /* clear the main buffer */
  42.     bufnew();
  43.     /* start off in command mode */
  44.     mode=CMNDMODE;
  45.     /* get null line 1 for edit() */
  46.     edgetln();
  47.     while(1){
  48.         if (mode == EXITMODE) {
  49.             break;
  50.         }
  51.         else if (mode == CMNDMODE) {
  52.             mode=command();
  53.         }
  54.         else if (mode == EDITMODE) {
  55.             mode=edit();
  56.         }
  57.         else if (mode == INSMODE) {
  58.             mode=insert();
  59.         }
  60.         else {
  61.             syserr("main: no mode");
  62.             mode=EDITMODE;
  63.         }
  64.     }
  65. }
  66.  
  67. /*
  68.  * handle edit mode.
  69.  * dispatch the proper routine based on one-character commands.
  70.  */
  71.  
  72. edit()
  73. {
  74. char buffer [SCRNW1];
  75. int v;
  76. int x,y;
  77. char c;
  78.     /* we can't do edgetln() or edgo() here because
  79.      * those calls reset the cursor.
  80.      */
  81.     pmtedit();
  82.     while(1){
  83.         /* get command */
  84.         c=tolower(syscin());
  85.  
  86.         if (c == ESC1 || c=='c') {
  87.             /* enter command mode. */
  88.             return(CMNDMODE);
  89.         }
  90.         else if (c == INS1 || c=='i') {
  91.             /* enter insert mode */
  92.             return(INSMODE);
  93.         }
  94.         else if (special(c) == YES) {
  95.             if (c == UP1 || c == DOWN1) {
  96.                 return(INSMODE);
  97.             }
  98.             else {
  99.                 continue;
  100.             }
  101.         }
  102.         else if (control(c) == YES) {
  103.             continue;
  104.         }
  105.         else if (c == ' ') {
  106.             edright();
  107.             pmtcol();
  108.         }
  109.         else if (c == 'b') {
  110.             edbegin();
  111.             pmtcol();
  112.         }
  113.         else if (c == 'd') {
  114.             /* scroll down */
  115.             pmtmode("edit: scroll");
  116.             while (bufnrbot() == NO) {
  117.                 if (chkkey() == YES) {
  118.                     break;
  119.                 }
  120.                 if (eddn() == ERR) {
  121.                     break;
  122.                 }
  123.             }
  124.             pmtedit();
  125.         }
  126.         else if (c == 'e') {
  127.             edend();
  128.             pmtcol();
  129.         }
  130.         else if (c == 'g') {
  131.             /* save x,y in case don't get number */
  132.             x=outgetx();
  133.             y=outgety();
  134.             pmtcmnd("edit: goto: ",buffer);
  135.             if(number(buffer,&v)) {
  136.                 edgo(v,0);
  137.             }
  138.             else {
  139.                 outxy(x,y);
  140.             }
  141.             pmtedit();
  142.         }
  143.         else if (c == 'k') {
  144.             pmtmode("edit: kill");
  145.             c=syscin();
  146.             if (special(c) == NO &&
  147.                 control(c) == NO) {
  148.                 edkill(c);
  149.             }
  150.             pmtedit();
  151.         }
  152.         else if (c == 's') {
  153.             pmtmode("edit: search");
  154.             c=syscin();
  155.             if (special(c) == NO &&
  156.                 control(c) == NO) {
  157.                 edsrch(c);
  158.             }
  159.             pmtedit();
  160.         }
  161.         else if (c == 'u') {
  162.             /* scroll up */
  163.             pmtmode("edit: scroll");
  164.             while (bufattop() == NO) {
  165.                 if (chkkey() == YES) {
  166.                     break;
  167.                 }
  168.                 if (edup() == ERR) {
  169.                     break;
  170.                 }
  171.             }
  172.             pmtedit();
  173.         }
  174.         else if (c == 'x') {
  175.             pmtmode("edit: eXchange");
  176.             c=syscin();
  177.             if (special(c) == NO &&
  178.                 control(c) == NO) {
  179.                 edchng(c);
  180.             }
  181.             pmtedit();
  182.         }
  183.         /* do nothing if command not found */
  184.     }
  185. }
  186.  
  187. /* insert mode.
  188.  * in this mode the UP1, UP2 keys reverse their roles,
  189.  * as do the DOWN1, and DOWN2 keys.
  190.  */
  191.  
  192. insert()
  193. {
  194. char c;
  195.     pmtmode("insert");
  196.     while (1) {
  197.         /* get command */
  198.         c=syscin();
  199.         if (c == ESC1) {
  200.             /* enter command mode */
  201.             return(CMNDMODE);
  202.         }
  203.         else if (c == EDIT1) {
  204.             /* enter edit mode */
  205.             return(EDITMODE);
  206.         }
  207.         else if (c == INS1) {
  208.             /* do nothing */
  209.             ;
  210.         }
  211.         else if (special(c) == YES) {
  212.             if (c == UP2 || c == DOWN2) {
  213.                 return(EDITMODE);
  214.             }
  215.             else {
  216.                 continue;
  217.             }
  218.         }
  219.         else if (control(c) == YES) {
  220.             /* ignore non-special control chars */
  221.             continue;
  222.         }
  223.         else {
  224.             /* insert one char in line */
  225.             edins(c);
  226.             pmtcol();
  227.         }
  228.     }
  229. }
  230.  
  231. /* return YES if c is a control char */
  232.  
  233. control(c) char c;
  234. {
  235.     if (c == TAB) {
  236.         return(NO);    /* tab is regular */
  237.     }
  238.     else if (c>=127) {
  239.         return(YES);    /* del or high bit on */
  240.     }
  241.     else if (c < 32) {
  242.         return(YES);    /* control char */
  243.     }
  244.     else {
  245.         return(NO);    /* normal */
  246.     }
  247. }
  248.  
  249. /*
  250.  * handle the default actions of all special keys.
  251.  * return YES if c is one of the keys.
  252.  */
  253.  
  254. special(c) char c;
  255. {
  256. int k;
  257.     if (c == JOIN1) {
  258.         edjoin();
  259.         pmtline();
  260.         return(YES);
  261.     }
  262.     if (c == SPLT1) {
  263.         edsplit();
  264.         pmtline();
  265.         return(YES);
  266.     }
  267.     if (c == ABT1) {
  268.         edabt();
  269.         pmtcol();
  270.         return(YES);
  271.     }
  272.     else if (c == DEL1) {
  273.         eddel();
  274.         pmtcol();
  275.         return(YES);
  276.     }
  277.     else if (c == ZAP1) {
  278.         edzap();
  279.         pmtline();
  280.         return(YES);
  281.     }
  282.     else if (c == UP2) {
  283.         /* move up */
  284.         edup();
  285.         pmtline();
  286.         return(YES);
  287.     }
  288.     else if (c == UP1) {
  289.         /* insert up */
  290.         ednewup();
  291.         pmtline();
  292.         return(YES);
  293.     }
  294.     else if (c == DOWN2) {
  295.         /* move down */
  296.         eddn();
  297.         pmtline();
  298.         return(YES);
  299.     }
  300.     else if (c == DOWN1) {
  301.         /* insert down */
  302.         ednewdn();
  303.         pmtline();
  304.         return(YES);
  305.     }
  306.     else if (c == LEFT1) {
  307.         edleft();
  308.         pmtcol();
  309.         return(YES);
  310.     }
  311.     else if (c == RIGHT1) {
  312.         edright();
  313.         pmtcol();
  314.         return(YES);
  315.     }
  316.     else {
  317.         return(NO);
  318.     }
  319. }
  320.  
  321. /*
  322.  * command() dispatches command routines while
  323.  * in command mode.
  324.  */
  325.  
  326. command()
  327. {
  328. int v;
  329. char c;
  330. char args [SCRNW1];
  331. char *argp;
  332. int topline;
  333. int ypos;
  334. int oldline;
  335. int k;
  336.     /* command mode commands may move the current line.
  337.      * command mode must save the current line on entry
  338.      * and restore it on exit.
  339.      */
  340.     edrepl();
  341.     /* remember how the screen was drawn on entry */
  342.     oldline=bufln();
  343.     ypos=outgety();
  344.     topline=oldline-ypos+1;
  345.     while(1) {
  346.         outxy(0,SCRNL1);
  347.         fmtcrlf();
  348.         pmtmode("command:");
  349.         getcmnd(args,0);
  350.         fmtcrlf();
  351.         pmtline();
  352.         c=args [0];
  353.         if (c == EDIT1 || c==INS1) {
  354.             /* redraw screen */
  355.             if (oldline == bufln()) {
  356.                 /* get current line */
  357.                 edgetln();
  358.                 /* redraw old screen */
  359.                 bufout(topline,1,SCRNL1);
  360.                 outxy(0,ypos);
  361.             }
  362.             else {
  363.                 /* update line and screen */
  364.                 edgo(bufln(),0);
  365.             }
  366.             if (c == EDIT1) {
  367.                 return (EDITMODE);
  368.             }
  369.             else {
  370.                 return (INSMODE);
  371.             }
  372.         }
  373.         else if (tolower(args [0]) == 'g'){
  374.             argp=skipbl(args+1);
  375.             if (argp [0] == EOS) {
  376.                 edgo(oldline,0); 
  377.                 return(EDITMODE);
  378.             }
  379.             else if (number(argp,&v) == YES) {
  380.                 edgo(v,0);
  381.                 return(EDITMODE);
  382.             }
  383.             else {
  384.                 message("bad line number");
  385.             }
  386.         }
  387.         else if (lookup(args,"append")) {
  388.             append(args);
  389.         }
  390.         else if (lookup(args,"change")) {
  391.             change(args);
  392.         }
  393.         else if (lookup(args,"clear")) {
  394.             clear();
  395.         }
  396.         else if (lookup(args,"delete")) {
  397.             delete(args);
  398.         }
  399.         else if (lookup(args,"dos")) {
  400.             if (chkbuf() == YES) {
  401.                 return (EXITMODE);
  402.             }
  403.         }
  404.         else if (lookup(args,"find")) {
  405.             if ((k = find()) >= 0) {
  406.                 edgo(bufln(),k);
  407.                 return(EDITMODE);
  408.             }
  409.             else {
  410.                 /* get current line */
  411.                 bufgo(oldline);
  412.                 edgetln();
  413.                 /* stay in command mode */
  414.                 message("pattern not found");
  415.             }
  416.         }
  417.         else if (lookup(args,"list")) {
  418.             list(args);
  419.         }
  420.         else if (lookup(args,"load")) {
  421.             load(args);
  422.         }
  423.         else if (lookup(args,"name")) {
  424.             name(args);
  425.         }
  426.         else if (lookup(args,"resave")) {
  427.             resave();
  428.         }
  429.         else if (lookup(args,"save")) {
  430.             save();
  431.         }
  432.         else if (lookup(args,"search")) {
  433.             search(args);
  434.         }
  435.         else if (lookup(args,"tabs")) {
  436.             tabs(args);
  437.         }
  438.         else {
  439.             message("command not found");
  440.         }
  441.     }
  442. }
  443.  
  444. /* return YES if line starts with command */
  445.  
  446. lookup(line,command) char *line, *command;
  447. {
  448.     while(*command) {
  449.         if (tolower(*line++) != *command++) {
  450.             return(NO);
  451.         }
  452.     }
  453.     if(*line == EOS || *line == ' ' || *line == TAB) {
  454.         return(YES);
  455.     }
  456.     else {
  457.         return(NO);
  458.     }
  459. }
  460.  
  461. /* get next command into argument buffer */
  462.  
  463. getcmnd(args,offset) char *args; int offset;
  464. {
  465. int j,k;
  466. char c;
  467.     outxy(offset,outgety());
  468.     outdeol();
  469.     k=0;
  470.     while ((c=syscin()) != CR) {
  471.         if (c == EDIT1 || c == INS1) {
  472.             args [0]=c;
  473.             return;
  474.         }
  475.         if (c == DEL1 || c == LEFT1) {
  476.             if (k>0) {
  477.                 outxy(offset,outgety());
  478.                 outdeol();
  479.                 k--;
  480.                 j=0;
  481.                 while (j < k) {
  482.                     outchar(args [j++]);
  483.                 }
  484.             }
  485.         }
  486.         else if (c == ABT1) {
  487.             outxy(offset,outgety());
  488.             outdeol();
  489.             k=0;
  490.         }
  491.         else if (c != TAB && (c < 32 || c == 127)) {
  492.             /* do nothing */
  493.             continue;
  494.         }
  495.         else {
  496.             if (k+offset < SCRNW1) {
  497.                 args [k++]=c;
  498.                 outchar(c);
  499.             }
  500.         }
  501.     }
  502.     args [k]=EOS;
  503. }
  504. 
  505.         }
  506.     }
  507. }
  508.  
  509. /* return YES i